home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 868 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.8 KB

  1. Path: news.bridge.net!news
  2. From: David Byrden <100101.2547@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Constructor Exceptions
  5. Date: 7 Jan 1996 22:35:08 GMT
  6. Organization: self-employed
  7. Message-ID: <4cphqs$hlc@news.bridge.net>
  8. References: <4bud9g$pv5@oxy.rust.net> <4cbhcl$kst@dawn.mmm.com> <4ce0da$513@oxy.rust.net>
  9. NNTP-Posting-Host: ppp-mia1-39.bridge.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
  14.  
  15.  
  16.   Paul;
  17.  
  18. When an exception is thrown from an object's constructor, that
  19. object's memory should be cleaned up, according to the draft
  20. standard. If it's a stack object, then of course this will 
  21. happen as the stack frame is erased. If it's a heap object, the
  22. 'new' keyword is supposed to take care of the problem.
  23.  
  24. Memory and resources which the constructor has already allocated 
  25. should be freed, of course, but this is the programmer's 
  26. responsibility. There are two means for doing so; first, wrapping
  27. every resource in a class so its destructor will free it;
  28. second, catching all exceptions at the end of the constructor
  29. with a catch(...) block, freeing resources there, and rethrowing.
  30.  
  31. The use of two-phase initialisation is not something I would 
  32. recommend. Templates will make it undesirable. How can you tell
  33. a template that your particular class needs init() called
  34. after the constructor?
  35.  
  36. However, note that many classes have a "null value", which is
  37. created by the default constructor, and typically an object
  38. with the null value will own no resources. This is not
  39. two-phase construction, although it looks similar.
  40.  
  41. Can you tell me offhand whether 'new' in VC2.1 is actually freeing 
  42. the heap memory of objects which throw?
  43.  
  44.  
  45.                 David Byrden
  46.  
  47.      ||  My opinions ARE those of my employer  ||
  48.  
  49.  
  50.